package com.erp.erp_servers.Dictionary.imp;

import com.erp.erp_dao.DictionaryValue.IDictionaryValueDAO;
import com.erp.erp_entitys.DictionaryKey.DictionaryKeyEntity;
import com.erp.erp_entitys.DictionaryValue.DictionaryValueEntity;
import com.erp.erp_servers.Dictionary.IDictionaryValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ImpDictionaryServer
 * @Description 字典服务实现类
 * @Author songjizhen
 * @Date 2020-06-22 14:49
 * @Version 1.0
 */
@Service
public class ImpDictionaryValueService implements IDictionaryValueService {

    @Autowired
    IDictionaryValueDAO dictionaryValueDAO;

    /**
     * 获取所有叶子节点的值，即右侧显示的表格内容
     * @param code
     * @return
     */
    @Override
    public List<DictionaryValueEntity> selectValueByParent(String code) {
       // String codes=dictionaryKeyDAO.selectCodesByParent(code);
        List<DictionaryValueEntity> list = dictionaryValueDAO.selectValueByParent(code);
      //  System.out.println(codes);
       // DictionaryValueEntity dv=dictionaryValueDAO.selectByPrimaryKey(2);
        return list;
    }
    /**
     * 更新父节点下所有叶子节点的值，即更新右侧的一条数据
     * @param list
     * @return
     */
    @Override
    public int updateValueByParent(List<DictionaryValueEntity> list) {
        int num=0;
        try {
            for(DictionaryValueEntity value:list){
               num+= dictionaryValueDAO.updateByPrimaryKey(value);
            }
            return num;
        }catch (Exception e){
            e.printStackTrace();
            return num;
        }
    }
    /**
     * 插入父节点下所有叶子节点的值，即右侧显示的一条数据
     * @param list
     * @return
     */
    @Override
    public int insertValueByParent(List<DictionaryValueEntity> list) {
        int num=0;
        try {
            for(DictionaryValueEntity value:list){
                num+= dictionaryValueDAO.insertSelective(value);
            }
            return num;
        }catch (Exception e){
            e.printStackTrace();
            return num;
        }
    }

    /**
     * 获取子节点值，即右侧一条数据
     *
     * @param list
     * @return
     */
    @Override
    public List<Map> getMap(String codes,List<DictionaryValueEntity> list) {
        String[] strs=codes.split(",");
        Map<Integer,Object> map=new HashMap<Integer, Object>();
        for(int i=0;i<list.size();i++){
            DictionaryValueEntity dve=list.get(i);
           map.put(dve.getIndex(),null);
        }
       List<Map> list_map=new ArrayList<Map>();
        for (Map.Entry<Integer, Object> entry : map.entrySet()) {
            Integer key=entry.getKey();
            Map mp=new HashMap();
            for(int i=0;i<list.size();i++){
                DictionaryValueEntity dve = list.get(i);
                if(key==dve.getIndex()){
                    mp.put(dve.getKeyid(),dve.getValue());
                }
            }
            list_map.add(mp);
            //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return list_map;
    }
}
